""" 读取文件"""
# f = open("d:\\upload.txt",encoding='utf-8')
# print(f.name)
# print(f.readline())
# print(f.tell())
# print("-----")
# print(f.read())
# print(f.tell())
#
# print("==========")
# print(f.seek(0,0))
# print(f.read(5))
# print(f.tell())
# print(list(f))
#迭代读取文件
# for each_line in f:
#     print(each_line)
# f.write("hello world\r\n")
# f.writelines(["one","two","1"])
# f.close()

"""写入文件"""
# mode 打开模式 ：w-以写入方式打开文件，会覆盖原文件
# a-以写入模式打开，如果文件存在，则在末尾追加写入
# f = open("d:\\upload.txt",mode="a",encoding='utf-8')
# f.write("这是一段待写入的数据\n")
# f.close()
def saveFile(count,boy,girl):
    filename_boy = 'd:\\boy_' + str(count) + '.txt'
    filename_girl = 'd:\\girl_' + str(count) + '.txt'
    boy_file = open(filename_boy, 'w',encoding='utf-8')
    girl_file = open(filename_girl, 'w',encoding='utf-8')
    boy_file.writelines(boy)
    girl_file.writelines(girl)

def splitFile(filepath):
    f = open(filepath, encoding='utf-8')
    boy = []
    girl = []
    count = 1
    for each_line in f:
        if each_line[:6] != '======':
            line = each_line.split('：', 1)
            role = line[0]
            spoke = line[1]
            if role == '小甲鱼':
                boy.append(spoke)
            if role == '小客服':
                girl.append(spoke)
        else:
            saveFile(count,boy,girl)
            boy.clear()
            girl.clear()
            count += 1
    saveFile(count,boy,girl)
    f.close()

# splitFile("d:\\upload.txt")





